<!--
 * @Description: 奖品
 * @Author: Boide
 * @Date: 2022-02-18 11:10:44
 * @LastEditors: guiyi@hongyi.tech
 * @LastEditTime: 2022-02-19 18:43:25
-->
<template>
  <el-card class="box-card">
    <el-form ref="queryForm" :model="query" :inline="true">
      <el-form-item label="">
        <el-input
          v-model.trim="query.name"
          placeholder="请输入昵称"
          clearable
          @change="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb20">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" @click="newcom()">新建</el-button>
      </el-col>
      <el-col :span="2.5">
        <el-button type="primary" icon="el-icon-plus" @click="handleDownload()">下载模板</el-button>
      </el-col>
      <el-col :span="2.5">
        <el-button type="primary" icon="el-icon-plus" @click="handleImport()">批量导入</el-button>
      </el-col>

      <input
        ref="import"
        style="display: none"
        type="file"
        @change="onFileChange"
        @click="onInputClick"
      />
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="dataList"
      stripe
      border
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="center" type="selection" width="50" />
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="实操读片得分" align="" prop="practicalOperationScore" />
      <el-table-column label="模拟诊疗得分" align="center" prop="analogDiagnosisScore" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          {{ row.status === 0 ? '未发布' : '已发布' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button type="text" v-if="row.status === 0" @click="release(row)">发布</el-button>
          <el-button type="text" v-else @click="cancelRelease(row)">取消发布</el-button>

          <template v-if="row.status === 0">
            <el-button type="text" @click="handleUpdate(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <div class="footer">
      <el-button @click="onRelease" type="primary"> 发布 </el-button>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageSize"
        @pagination="handleQuery"
      />
    </div>

    <!-- 添加或修改菜单 -->
    <el-dialog
      :title="(formModel.id ? '编辑' : '新增') + '奖品'"
      :visible.sync="formVisible"
      width="600px"
      append-to-body
    >
      <el-form ref="formModel" :model="formModel" :rules="formModelRules" label-width="150px">
        <el-form-item label="奖品名称:" prop="name">
          <el-input
            v-model.trim="formModel.name"
            style="width: 200px"
            clearable
            placeholder="请输入标签名称"
            maxlength="50"
          />
        </el-form-item>
        <el-form-item label="奖品图片:" prop="img">
          <upload-img v-model="formModel.img" />
        </el-form-item>
        <el-form-item label="分类:" prop="type">
          <el-select v-model="formModel.type" placeholder="请选择">
            <el-option label="通关奖品" :value="0"></el-option>
            <el-option label="全国排名top10" :value="1"></el-option>
            <el-option label="全国排名top3" :value="2"></el-option>
            <el-option label="三项通过" :value="3"></el-option>
            <el-option label="决赛" :value="4"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            formVisible = !formVisible
            reset
          "
          >取 消</el-button
        >
        <!-- <el-button :loading="submitLoading" type="primary" @click="submitForm">确 定</el-button> -->
      </div>
    </el-dialog>
    <el-dialog
      :title="
        newcompete.actualScoreId == '' ? '皮肤病大赛 - 实战比拼编辑' : '皮肤病大赛 - 实战比拼新建'
      "
      center
      :visible.sync="newVisible"
      width="600px"
      append-to-body
    >
      <el-form
        :label-position="labelPosition"
        label-width="150px"
        :model="newcompete"
        ref="newcompete"
        :rules="rules"
      >
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="newcompete.nickname"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="newcompete.mobile"></el-input>
        </el-form-item>
        <el-form-item label="实操读片得分" prop="practicalOperationScore">
          <el-input
            v-model="newcompete.practicalOperationScore"
            :disable="newcompete.actualScoreId == '' ? 'true' : 'false'"
          ></el-input>
        </el-form-item>
        <el-form-item label="模拟诊疗得分" prop="analogDiagnosisScore">
          <el-input v-model="newcompete.analogDiagnosisScore"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onCancel">取 消</el-button>
        <el-button :loading="submitLoading" type="primary" @click="submitForm('newcompete')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import apis from '@/api/skinDisease/compete/competeApis.js'

export default {
  name: 'index',
  data() {
    return {
      labelPosition: 'left',
      total: 0,
      // 列表数据
      query: {
        pageNum: 1,
        pageSize: 10
      },
      dataList: [],
      listLoading: false,
      // 新增/编辑用户
      formVisible: false,
      submitLoading: false,
      formModel: {},
      formModelRules: {
        name: [{ required: true, message: '奖品名称不能为空', trigger: 'blur' }]
      },
      newVisible: false,
      newcompete: {},
      type: 'add',
      rules: {
        nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        practicalOperationScore: [
          { required: true, message: '请输入实操读片得分', trigger: 'blur' }
        ],
        analogDiagnosisScore: [{ required: true, message: '请输入模拟诊疗得分', trigger: 'blur' }]
      },
      selectRows: []
    }
  },
  methods: {
    newcom() {
      this.newcompete = {}
      this.newVisible = true
    },
    handleQuery() {
      this.listLoading = true
      let params = { ...this.query }
      apis
        .cpScoreList(params)
        .then(res => {
          this.dataList = res.data.records
          this.total = res.data.total
          this.listLoading = false
        })
        .catch(err => {
          if (err.code === -103) {
            this.$router.push('/403')
          }
          this.listLoading = false
        })
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleAdd(row) {
      this.reset()
      if (row) {
        this.formModel = { ...row }
      }
      this.formVisible = !this.formVisible
    },
    cancelRelease(row) {
      let arr_ = new Array()
      arr_.push(row.actualScoreId)

      let temp = {
        actualScoreIds: arr_,
        status: 0
      }

      apis.cpScorePub(temp).then(res => {
        // console.log(res)
        if (res.code == 0) {
          this.handleQuery()
          this.$message({
            message: res.message,
            type: 'success'
          })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleUpdate(row) {
      this.newVisible = true
      this.type = 'edit'
      this.newcompete = {
        actualScoreId: row.actualScoreId,
        nickname: row.nickName,
        mobile: row.mobile,
        practicalOperationScore: row.practicalOperationScore,
        analogDiagnosisScore: row.analogDiagnosisScore
      }
      console.log('row', row)
    },
    handleDelete(row) {
      apis.cpScoreDel(row.actualScoreId).then(res => {
        // console.log(res)
        if (res.code == 0) {
          this.handleQuery()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message.error(res.message)
        }
      })
    },
    release(data) {
      this.$confirm('确认发布吗').then(() => {
        apis.cpScorePub({ actualScoreIds: [data.actualScoreId], status: 0 }).then(() => {
          this.$message.success('发布成功')
          this.handleQuery()
        })
      })
    },
    onRelease() {
      apis
        .cpScorePub({ actualScoreIds: this.selectRows.map(v => v.actualScoreId), status: 0 })
        .then(() => {
          this.$message.success('发布成功')
          this.handleQuery()
        })
    },
    // 新增/编辑
    submitForm(newcompete) {
      this.submitLoading = true
      console.log(newcompete)
      this.$refs['newcompete'].validate(async valid => {
        if (valid) {
          if (this.type === 'edit') {
            apis.cpScoreEdit(this.newcompete).then(() => {
              this.handleQuery()
              this.newVisible = false
            })
          } else {
            apis.cpScoreAdd(this.newcompete).then(() => {
              this.handleQuery()
              this.newVisible = false
            })
          }

          this.submitLoading = false
        } else {
          this.submitLoading = false
        }
      })
    },
    onCancel() {
      this.newVisible = false
    },
    handleSelectionChange(v) {
      this.selectRows = v
    },
    // 表单重置
    reset() {
      this.formModel = {}
      this.resetForm('formModel')
    },
    handleDownload() {
      apis.download().then(res => {
        window.open(res.data)
      })
    },
    handleImport() {
      this.$refs.import.click()
    },
    onInputClick(e) {
      e.target.value = ''
    },
    onFileChange(e) {
      if (!e.target.files[0]) {
        this.$message.warning('请选择文件')
        return
      }
      console.log(e.target.files[0])
      const formData = new FormData()
      formData.append('file', e.target.files[0])
      formData.append('excelFile', e.target.files[0])

      console.log(formData, 'formData')
      apis.cpScoreImp(formData).then(() => {
        this.$message.success('导入成功')
      })
    }
  },
  mounted() {
    this.query = {}
    this.handleQuery()
  }
}
</script>

<style scoped>
.dh {
  width: 0;
  height: 100px;
  background-color: #1c84c6;
  transition: all 1s;
}
.dh.show {
  width: 200px;
}
.dh.hide {
  width: 0;
}

.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 30px;
}

.pagination-container {
  margin-top: 0;
}
</style>
